package com.edou.system.controller.admin;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.auth.entity.User;
import com.edou.auth.service.IUserService;
import com.edou.base.controller.BaseController;
import com.edou.base.token.TokenValidate;
import com.edou.base.util.NumberUtil;
import com.edou.system.entity.SysDict;
import com.edou.system.entity.SysDictItem;
import com.edou.system.form.SysDictForm;
import com.edou.system.form.SysDictItemForm;
import com.edou.system.service.ISysDictItemService;
import com.edou.system.service.ISysDictService;

/**
 * 数据库字典项明细
 * @creator     admin
 * @create-time 2016年3月28日   下午2:32:44
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Controller("sysDictItemController")
@Scope("prototype")
@RequestMapping(value = "/admin/system/dictItem")
public class SysDictItemController extends BaseController{
	
	@Resource(name = "sysDictServiceImpl")
	private ISysDictService sysDictService;
	@Resource(name = "sysDictItemServiceImpl")
	private ISysDictItemService sysDictItemService;
	@Resource(name = "userServiceImpl")
	private IUserService userService;


	/**
	 * 数据字典项明细
	 * 
	 * @return
	 */
	@RequestMapping(value = "/listSysDictItemByPage.htm", method = RequestMethod.POST)
	public void listSysDictItemByPage(@ModelAttribute SysDictItemForm model, HttpServletRequest request, HttpServletResponse response) {
		try{
			sysDictItemService.listDictItemByPage(model.getPageResult(),model.getCondition());
			JSONObject result = toPageJson(model.getPageResult(), new String[] {"id", "name","code","sort"});
			ajaxPageResult(result, response);
		}catch(Exception ex){
			String msg = "查询数据字典项发生异常：" + ex.getMessage();
			logger.error(msg,ex);
		}
	}

	/**
	 * 跳转编辑数据库字典
	 * 
	 * @return
	 */
	@RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
	public ModelAndView edit(@ModelAttribute SysDictItemForm model, HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_dict_item_edit");
		Map<String, Object> data = view.getModel();
		
		if(null != model.getItem().getDictId()){
			SysDict sysDict = sysDictService.getSysDictById(model.getItem().getDictId());
			data.put("sysDict", sysDict);
		}
		
		if (null != model.getItem().getId()) {
			SysDictItem item = sysDictItemService.getSysDictItemById(model.getItem().getId());
			data.put("item", item);
		}
		return view;
	}

	/**
	 * 保存数据字典项
	 * 
	 * @return
	 */
	@RequestMapping(value = "/saveDictItem.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView saveDictItem(@ModelAttribute SysDictItemForm model,HttpServletRequest request, HttpServletResponse response) {
		try {
			User currentUser = userService.getCurrentUser();
			model.getItem().setCreateBy(currentUser.getAccount());
			model.getItem().setUpdateBy(currentUser.getAccount());
			
			if(null == model.getItem().getId()){
				sysDictItemService.add(model.getItem());
			}else{
				sysDictItemService.update(model.getItem());
			}
			
			return ajaxJSON(Status.success,"保存成功",true,request);
		} catch (Exception ex) {
			String message = "编辑数据库字典时发生异常:" + ex.getMessage();
			logger.error(message, ex);
			return ajaxJSON(Status.error, message,true,request);
		}
	}

	/**
	 * 批量删除数据库字典项
	 * 
	 * @return
	 */
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public ModelAndView del(@ModelAttribute SysDictForm model,HttpServletRequest request, HttpServletResponse response) {
		try {
			sysDictItemService.delByIds(NumberUtil.convertStringArrayToLongArray(model.getSelIds().split(",")));
			return ajaxJSON(Status.success);
		} catch (Exception e) {
			String message = "批量删除数据库字典项时发生异常：" + e.getMessage();
			logger.error(message, e);
			return ajaxJSON(Status.error, message);
		}
	}
}
